//
//  KVNMineViewController.m
//  Test1
//
//  Created by KelvinYeung on 15/8/17.
//  Copyright (c) 2015年 KelvinYeung. All rights reserved.
//

#import "KVNMineViewController.h"

@interface KVNMineViewController ()

@property UITableView * tableView;
@property UIView * headerView;
@property NSArray *optionData;
@end

@implementation KVNMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *mineBarButtonItem = [[UIBarButtonItem alloc] init];
    [mineBarButtonItem setTitle:@"我的"];
    mineBarButtonItem.tintColor = [UIColor whiteColor];
    [self.navigationItem setLeftBarButtonItem:mineBarButtonItem];
    
    
    CGRect appFrame = [[UIScreen mainScreen] bounds];
    _optionData = @[
                        @[
                            @{@"title":@"我的钱包",@"icon":@"mypurse"},
                            @{@"title":@"我的订单",@"icon":@"myorder"},
                            @{@"title":@"我的收藏",@"icon":@"mycollect"}
                        ],
                        @[
                            @{@"title":@"推荐应用",@"icon":@"recommendapp"},
                            @{@"title":@"我的工具",@"icon":@"mytool"},
                            @{@"title":@"设置",@"icon":@"mysetting"}
                            ]
                    ];
    
    
    
    //个人头像区-------------------
    _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, appFrame.size.width, 230)];
    _headerView.backgroundColor = [UIColor purpleColor];
    
    //头像区
    UIView *avatarBanner = [[UIView alloc] initWithFrame:CGRectMake(0, 0, appFrame.size.width, 140)];
    avatarBanner.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    [_headerView addSubview:avatarBanner];
    
    //banner背景图
    UIImageView *avatarBannerBackgroundView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,  appFrame.size.width, 140)];
    
    [avatarBannerBackgroundView setImage:[UIImage imageNamed:@"userBanner.jpg"]];
    [avatarBanner addSubview:avatarBannerBackgroundView];
    //头像
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 30, 80, 80)];
    [imageView setImage:[UIImage imageNamed:@"userAvatar.jpg"]];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.clipsToBounds = YES;
    imageView.layer.cornerRadius = 40;
    imageView.layer.borderColor= [[UIColor whiteColor] CGColor];
    imageView.layer.borderWidth = 2;
    [avatarBanner addSubview:imageView];
    
    UILabel *userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(110, 50, 120, 40)];
    userNameLabel.text = @"小皖熊";
    userNameLabel.font = [UIFont systemFontOfSize:24];
    [avatarBanner addSubview:userNameLabel];
    
    
    UIButton *avatarBannerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    avatarBannerButton.frame = CGRectMake(appFrame.size.width - 20 - 40, 50, 40, 40);
    avatarBannerButton.tintColor = [UIColor blackColor];
    [avatarBannerButton setImage:[UIImage imageNamed:@"my_image_turn_right_bg"] forState:UIControlStateNormal];
    
    [avatarBanner addSubview:avatarBannerButton];
    [avatarBannerButton addTarget:self action:@selector(handleMyButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    avatarBannerButton.tag = KVN_FAKE_VIEW_MY_DETAIL;
    
    
    
    //工具栏区------------------------------
    UIView *headerToolView = [[UIView alloc]initWithFrame:CGRectMake(0, avatarBanner.frame.size.height, appFrame.size.width, 90)];
    headerToolView.backgroundColor = [UIColor greenColor];
    
    [_headerView addSubview:headerToolView];
    
    
    //消息按钮
    UIButton *msgButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [msgButton setImage:[UIImage imageNamed:@"mymsg"] forState:UIControlStateNormal];
    [msgButton setTitle:@"消息" forState:UIControlStateNormal];
    [msgButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    msgButton.tintColor =[UIColor blackColor];
    msgButton.backgroundColor = [UIColor whiteColor];
    msgButton.titleLabel.font = [UIFont systemFontOfSize:14];
    msgButton.frame = CGRectMake(0, 0, headerToolView.frame.size.width/4.0, headerToolView.frame.size.height);
    msgButton.imageEdgeInsets = UIEdgeInsetsMake(msgButton.frame.size.height*0.2, msgButton.frame.size.width/3.0, msgButton.frame.size.height*0.45, msgButton.frame.size.width/3.0);
    
    msgButton.titleEdgeInsets = UIEdgeInsetsMake(msgButton.frame.size.height*0.45, -msgButton.frame.size.width/4.0, 0, msgButton.frame.size.width/4.0);
    [headerToolView addSubview:msgButton];
    msgButton.enabled=YES;
    [msgButton addTarget:self action:@selector(handleMyButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    msgButton.tag = KVN_FAKE_VIEW_MY_MSG;
    
    
    
    //通话按钮
    UIButton *callButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [callButton setImage:[UIImage imageNamed:@"mytel"] forState:UIControlStateNormal];
    [callButton setTitle:@"通话" forState:UIControlStateNormal];
    [callButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    callButton.tintColor =[UIColor blackColor];
    callButton.backgroundColor = [UIColor whiteColor];
    callButton.titleLabel.font = [UIFont systemFontOfSize:14];
    callButton.frame = CGRectMake(headerToolView.frame.size.width/4.0, 0,headerToolView.frame.size.width/4.0, headerToolView.frame.size.height);
    callButton.imageEdgeInsets = UIEdgeInsetsMake(callButton.frame.size.height*0.2, callButton.frame.size.width/3.0, callButton.frame.size.height*0.45, callButton.frame.size.width/3.0);
    
    callButton.titleEdgeInsets = UIEdgeInsetsMake(callButton.frame.size.height*0.45, -callButton.frame.size.width/4.0, 0, callButton.frame.size.width/4.0);
    [headerToolView addSubview:callButton];
    [callButton addTarget:self action:@selector(handleMyButtonClick:) forControlEvents:UIControlEventTouchUpInside];
     callButton.tag = KVN_FAKE_VIEW_MY_CALL;
    
    //预约按钮
    UIButton *dateButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [dateButton setImage:[UIImage imageNamed:@"myreservation"] forState:UIControlStateNormal];
    [dateButton setTitle:@"预约" forState:UIControlStateNormal];
    [dateButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    dateButton.tintColor =[UIColor blackColor];
    dateButton.backgroundColor = [UIColor whiteColor];
    dateButton.titleLabel.font = [UIFont systemFontOfSize:14];
    dateButton.frame = CGRectMake(headerToolView.frame.size.width/2.0,0, headerToolView.frame.size.width/4.0, headerToolView.frame.size.height);
    dateButton.imageEdgeInsets = UIEdgeInsetsMake(dateButton.frame.size.height*0.2, dateButton.frame.size.width/3.0, dateButton.frame.size.height*0.45, dateButton.frame.size.width/3.0);
    
    dateButton.titleEdgeInsets = UIEdgeInsetsMake(dateButton.frame.size.height*0.45, -dateButton.frame.size.width/4.0, 0, dateButton.frame.size.width/4.0);
    [headerToolView addSubview:dateButton];
     [dateButton addTarget:self action:@selector(handleMyButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    dateButton.tag = KVN_FAKE_VIEW_MY_DATE;
    
    //评价按钮
    UIButton *commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [commentButton setImage:[UIImage imageNamed:@"myevaluate"] forState:UIControlStateNormal];
    [commentButton setTitle:@"评价" forState:UIControlStateNormal];
    [commentButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    commentButton.tintColor =[UIColor blackColor];
    commentButton.backgroundColor = [UIColor whiteColor];
    commentButton.titleLabel.font = [UIFont systemFontOfSize:14];
    commentButton.frame = CGRectMake(headerToolView.frame.size.width*0.75,0,headerToolView.frame.size.width/4.0, headerToolView.frame.size.height);
    commentButton.imageEdgeInsets = UIEdgeInsetsMake(commentButton.frame.size.height*0.2, commentButton.frame.size.width/3.0, commentButton.frame.size.height*0.45, commentButton.frame.size.width/3.0);
    
    commentButton.titleEdgeInsets = UIEdgeInsetsMake(commentButton.frame.size.height*0.45, -commentButton.frame.size.width/4.0, 0, commentButton.frame.size.width/4.0);
    [headerToolView addSubview:commentButton];
     [commentButton addTarget:self action:@selector(handleMyButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    commentButton.tag = KVN_FAKE_VIEW_MY_COMMENT;
    
    /*
    //分隔线1
    UIView *seperator1  = [[UIView alloc]initWithFrame:CGRectMake(headerToolView.frame.size.width*0.25, headerToolView.frame.size.height*0.25, 1, headerToolView.frame.size.height*0.5)];
    seperator1.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    [headerToolView addSubview:seperator1];
    
    //分隔线2
    UIView *seperator2  = [[UIView alloc]initWithFrame:CGRectMake(headerToolView.frame.size.width*0.5, headerToolView.frame.size.height*0.25, 1, headerToolView.frame.size.height*0.5)];
    seperator2.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    [headerToolView addSubview:seperator2]; //分隔线1
    UIView *seperator3  = [[UIView alloc]initWithFrame:CGRectMake(headerToolView.frame.size.width*0.75, headerToolView.frame.size.height*0.25, 1, headerToolView.frame.size.height*0.5)];
    seperator3.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    [headerToolView addSubview:seperator3]; //分隔线1
 */
    
    //tableView -------------------------------------
    _tableView = [[UITableView alloc] initWithFrame:appFrame];
    self.view = _tableView;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableHeaderView = _headerView;
    
    
    
    //去掉多余分割线
    UIView *footView = [[UIView alloc] initWithFrame:CGRectZero];
    [_tableView setTableFooterView:footView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


- (void) handleMyButtonClick:(UIButton *)sender{
  
    switch (sender.tag) {
        case KVN_FAKE_VIEW_MY_DETAIL:
        {
            KVNFakeViewController *controller = [[KVNFakeViewController alloc] init];
            controller.controllerType = KVN_FAKE_VIEW_MY_DETAIL;
            UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] init];
            [buttonItem setTitle:@"个人资料"];
            self.navigationItem.backBarButtonItem = buttonItem;
            [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
            [self.navigationController pushViewController:controller animated:YES];
            
        }
            break;
        case KVN_FAKE_VIEW_MY_MSG:
        {
            KVNFakeViewController *controller = [[KVNFakeViewController alloc] init];
            controller.controllerType = KVN_FAKE_VIEW_MY_MSG;
            UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] init];
            [buttonItem setTitle:@"聊天消息"];
            self.navigationItem.backBarButtonItem = buttonItem;
            [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
            [self.navigationController pushViewController:controller animated:YES];
        }
            break;
        case KVN_FAKE_VIEW_MY_CALL:
        {
            KVNFakeViewController *controller = [[KVNFakeViewController alloc] init];
            controller.controllerType = KVN_FAKE_VIEW_MY_CALL;
            UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] init];
            [buttonItem setTitle:@"400通话记录"];
            self.navigationItem.backBarButtonItem = buttonItem;
            [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
            [self.navigationController pushViewController:controller animated:YES];
        }
            break;
        case KVN_FAKE_VIEW_MY_DATE:
        {
            KVNFakeViewController *controller = [[KVNFakeViewController alloc] init];
            controller.controllerType = KVN_FAKE_VIEW_MY_DATE;
            UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] init];
            [buttonItem setTitle:@"我的预约"];
            self.navigationItem.backBarButtonItem = buttonItem;
            [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
            [self.navigationController pushViewController:controller animated:YES];
        }
            break;
        case KVN_FAKE_VIEW_MY_COMMENT:
        {
            KVNFakeViewController *controller = [[KVNFakeViewController alloc] init];
            controller.controllerType = KVN_FAKE_VIEW_MY_COMMENT;
            UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] init];
            [buttonItem setTitle:@"评价管理"];
            self.navigationItem.backBarButtonItem = buttonItem;
            [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
            [self.navigationController pushViewController:controller animated:YES];
        }
            break;
        default:
            break;
    }
    
}

#pragma mark - tableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  [[_optionData objectAtIndex:section] count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return _optionData.count;
}


- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KVN_MINE_PROFILE_OPTION_CELL"];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"KVN_MINE_PROFILE_OPTION_CELL"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.imageView.translatesAutoresizingMaskIntoConstraints = YES;
        [cell.imageView setFrame:CGRectMake(0, 0, 10, 10)];

    }
    
    cell.textLabel.text = [[[_optionData objectAtIndex:indexPath.section]objectAtIndex:indexPath.row] valueForKey:@"title"];
    [cell.imageView setImage:[UIImage imageNamed:[[[_optionData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] valueForKey:@"icon"]]];
    
    return cell;
}

- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @" ";
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    switch (indexPath.section) {
        case 0:
        {
            switch (indexPath.row) {
                case 0:
                {
                    KVNFakeViewController *controller = [[KVNFakeViewController alloc] init];
                    controller.controllerType = KVN_FAKE_VIEW_MY_WALLET;
                    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] init];
                    [buttonItem setTitle:@"我的钱包"];
                    self.navigationItem.backBarButtonItem = buttonItem;
                    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
                    [self.navigationController pushViewController:controller animated:YES];
                    
                }
                    break;
                case 1:
                {
                    KVNFakeViewController *controller = [[KVNFakeViewController alloc] init];
                    controller.controllerType = KVN_FAKE_VIEW_MY_ORDER;
                    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] init];
                    [buttonItem setTitle:@"我的订单"];
                    self.navigationItem.backBarButtonItem = buttonItem;
                    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
                    [self.navigationController pushViewController:controller animated:YES];
                }
                    break;
                case 2:
                {
                    KVNFakeViewController *controller = [[KVNFakeViewController alloc] init];
                    controller.controllerType = KVN_FAKE_VIEW_MY_FAVOR;
                    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] init];
                    [buttonItem setTitle:@"我的收藏"];
                    self.navigationItem.backBarButtonItem = buttonItem;
                    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
                    [self.navigationController pushViewController:controller animated:YES];
                }
                    break;
                default:
                    break;
            }
            
        }
            
            break;
        case 1:
        {
            switch (indexPath.row) {
                case 0:
                {
                    KVNFakeViewController *controller = [[KVNFakeViewController alloc] init];
                    controller.controllerType = KVN_FAKE_VIEW_MY_APP;
                    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] init];
                    [buttonItem setTitle:@"推荐应用"];
                    self.navigationItem.backBarButtonItem = buttonItem;
                    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
                    [self.navigationController pushViewController:controller animated:YES];
                }
                    break;
                case 1:
                {
                    KVNFakeViewController *controller = [[KVNFakeViewController alloc] init];
                    controller.controllerType = KVN_FAKE_VIEW_MY_TOOL;
                    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] init];
                    [buttonItem setTitle:@"常用工具"];
                    self.navigationItem.backBarButtonItem = buttonItem;
                    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
                    [self.navigationController pushViewController:controller animated:YES];
                }
                    break;
                case 2:
                {
                    KVNFakeViewController *controller = [[KVNFakeViewController alloc] init];
                    controller.controllerType = KVN_FAKE_VIEW_MY_SETTING;
                    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] init];
                    [buttonItem setTitle:@"我设置"];
                    self.navigationItem.backBarButtonItem = buttonItem;
                    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
                    [self.navigationController pushViewController:controller animated:YES];
                }
                    break;
                default:
                    break;
            }
            
        }
            break;
        default:
            break;
    }

}



-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.navigationController.tabBarController.tabBar setHidden:NO ];
    [_tableView reloadData];
   
}

@end
